package com.coderush.admin.sys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coderush.admin.core.common.MyBatisPlusPageUtils;
import com.coderush.admin.core.common.PageResult;
import com.coderush.admin.core.component.SnowflakeIdGenerator;
import com.coderush.admin.core.utils.BeanConvertUtil;
import com.coderush.admin.sys.dao.DepartmentDao;
import com.coderush.admin.sys.exception.DepartmentException;
import com.coderush.admin.sys.model.dto.DepartmentAddDto;
import com.coderush.admin.sys.model.dto.DepartmentUpdateDto;
import com.coderush.admin.sys.model.entity.Company;
import com.coderush.admin.sys.model.entity.Department;
import com.coderush.admin.sys.model.vo.CompanyPageVo;
import com.coderush.admin.sys.model.vo.DepartmentPageVo;
import com.coderush.admin.sys.model.vo.DepartmentVo;
import com.coderush.admin.sys.service.DepartmentService;
import com.coderush.admin.sys.mapper.DepartmentMapper;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【sys_department(部门表)】的数据库操作Service实现
* @createDate 2025-08-25 11:11:32
*/
@Service
@AllArgsConstructor
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService{

    @Resource
    private SnowflakeIdGenerator snowflakeIdGenerator;

    private final DepartmentDao departmentDao;

    /**
     * 分页查询部门列表
     * @param deptName 部门名称
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return 部门列表
     */
    @Override
    public PageResult<DepartmentPageVo> findByPage(String deptName, Integer pageNum, Integer pageSize) {

        // 构建分页对象
        Page<Department> page = MyBatisPlusPageUtils.buildPage(pageNum, pageSize);
        // 执行分页查询（会自动分页）
        Page<Department> usePage = departmentDao.findByPage(page, deptName);
        // 将 MyBatis-Plus 的 IPage 转换为统一的 PageResult
        PageResult<Department> result = MyBatisPlusPageUtils.toPageResult(usePage);
        result.setList(usePage.getRecords());

        // 数据模型转换 将 entity 转为 vo
        PageResult<DepartmentPageVo> pageResult = new PageResult<>();
        pageResult.setTotal(result.getTotal());
        pageResult.setPages(result.getPages());
        pageResult.setPageNum(result.getPageNum());
        pageResult.setPageSize(result.getPageSize());
        List<DepartmentPageVo> voList = new ArrayList<>();

        if (result.getList() != null && result.getList().size() > 0) {
            for (Department department : result.getList()) {
                var departmentPageVo = JSON.parseObject(JSON.toJSONString(department), DepartmentPageVo.class);
                BeanConvertUtil.fieldConvert(department, departmentPageVo);
                voList.add(departmentPageVo);
            }
        }

        pageResult.setList(voList);

        return pageResult;
    }

    /**
     * 根据ID查询部门
     * @param id 部门ID
     * @return 部门信息
     */
    @Override
    public DepartmentVo getById(Long id) {

        // 根据ID查询
        var department = departmentDao.getById(id);
        if (department == null) {
            throw DepartmentException.departmentNotExist();
        }

        var departmentVo = JSON.parseObject(JSON.toJSONString(department), DepartmentVo.class);
        return departmentVo;
    }

    /**
     * 更新部门
     * @param updateDto 数据模型
     * @return true:更新成功,false:更新失败
     */
    @Override
    public Boolean update(DepartmentUpdateDto updateDto) {

        // 根据ID查询
        Department departmentDB = departmentDao.getById(updateDto.getId());
        if (departmentDB == null) {
            throw DepartmentException.departmentNotExist();
        }

        Department department = JSON.parseObject(JSON.toJSONString(updateDto), Department.class);
        return departmentDao.update(department) > 0;
    }

    /**
     * 添加部门
     * @param addDto 数据模型
     * @return true:添加成功,false:添加失败
     */
    @Override
    public Boolean add(DepartmentAddDto addDto) {

        // 部门编码不能重复
        if (departmentDao.getByDeptCode(addDto.getDeptCode()) != null) {
            throw DepartmentException.repeatDeptCode();
        }
        // 部门名称不能重复
        if (departmentDao.getByDeptName(addDto.getCompanyId(), addDto.getDeptName()) != null) {
            throw DepartmentException.repeatDepartmentName();
        }
        var department = JSON.parseObject(JSON.toJSONString(addDto), Department.class);
        department.setId(snowflakeIdGenerator.nextId());

        return departmentDao.add(department) > 0;
    }
}




